package top.hzy520.role.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hzy520.role.dao.RoleMapper;
import top.hzy520.role.dto.PermissionOutPutDto;
import top.hzy520.role.dto.RoleInputDto;
import top.hzy520.role.entity.Role;
import top.hzy520.user.dao.PermissionMapper;
import top.hzy520.user.entity.Permission;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 19:35 2018/9/27
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public int selectAllRoleCount() {
        return roleMapper.selectAllRoleCount();
    }

    @Override
    public List<RoleInputDto> selectAllRole(int offset, int limit) {
        List<Role>  roleList= roleMapper.selectAllRole(offset,limit);
        List<RoleInputDto> roleInputDtoList=new ArrayList<>();
           roleList.stream().forEach(role -> {
               RoleInputDto roleInputDto=new RoleInputDto();
               if (role.getParentId()!=null){
                   Role parentRole=roleMapper.selectByPrimaryKey(role.getParentId());
                   roleInputDto.setParentRoleName(parentRole.getRoleDescription());
               }
               roleInputDto.setRoleId(role.getRoleId());
               roleInputDto.setRole(role.getRole());
               roleInputDto.setRoleDescription(role.getRoleDescription());
               roleInputDtoList.add(roleInputDto);
           });
         return roleInputDtoList;
    }

    @Override
    public Role selectRoleById(Long roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public List<PermissionOutPutDto> treePermissionList(Long roleId) {
        List<Permission> permissionList= permissionMapper.selectAll();
        List<Permission> currentRolePermission=roleMapper.selectById(roleId).getPermissionList();
        List<PermissionOutPutDto> permissionOutPutDtoList =new ArrayList<>();
        permissionList.stream().forEach(permission -> {
            PermissionOutPutDto permissionOutPutDto=new PermissionOutPutDto();
            currentRolePermission.stream().forEach(param->{
                if (param.getPermission().equals(permission.getPermission())){
                   permissionOutPutDto.setChecked(true);
                }
            });
            permissionOutPutDto.setId(permission.getPermissionId());
            permissionOutPutDto.setName(permission.getPermissionDescription());
            permissionOutPutDto.setPId(permission.getPId());
            permissionOutPutDto.setPermission(permission.getPermission());
            permissionOutPutDtoList.add(permissionOutPutDto);
        });
        return permissionOutPutDtoList;
    }

    @Override
    public void roleEdit(List<PermissionOutPutDto> permissionOutPutDtoList, Long roleId) {
        roleMapper.deleteByRoleId(roleId);
        permissionOutPutDtoList.stream().forEach(param->{
            roleMapper.insertRole(roleId,param.getId());
        });
    }

    @Override
    public List<Role> selectRole() {
        return roleMapper.selectRole();
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void roleAdd(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void insertRole(Long roleId, Long userId) {
        roleMapper.insertRoleAndUser(userId,roleId);
    }

    @Override
    public void updateUserRole(Long roleId, Long userId) {
        roleMapper.updateUserRole(roleId,userId);
    }

}
